package org.example.service;

import java.sql.*;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

public class DatabaseService {
    private static final String DB_PATH = "apk_dependencies.db";

    public void initDatabase() {
        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
             Statement stmt = conn.createStatement()) {
            // 创建依赖表
            stmt.execute("CREATE TABLE IF NOT EXISTS dependencies (" +
                    "apk_hash TEXT, apk_name TEXT, package_name TEXT, version TEXT, " +
                    "PRIMARY KEY (apk_hash, package_name))");
            // 创建任务状态表
            stmt.execute("CREATE TABLE IF NOT EXISTS task_status (" +
                    "apk_hash TEXT PRIMARY KEY, status TEXT, retry_count INTEGER)");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void saveDependencies(String apkHash, String apkName, Map<String, String> deps) {
        String sql = "INSERT OR IGNORE INTO dependencies VALUES (?, ?, ?, ?)";
        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            conn.setAutoCommit(false); // 开启事务
            
            for (Map.Entry<String, String> entry : deps.entrySet()) {
                pstmt.setString(1, apkHash);
                pstmt.setString(2, apkName);
                pstmt.setString(3, entry.getKey());
                pstmt.setString(4, entry.getValue());
                pstmt.addBatch();
            }
            
            pstmt.executeBatch();
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    // 新增：批量保存多个APK的依赖关系
    public void saveDependenciesBatch(List<DependencyEntry> entries) {
        String sql = "INSERT OR IGNORE INTO dependencies VALUES (?, ?, ?, ?)";
        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            conn.setAutoCommit(false); // 开启事务
            
            for (DependencyEntry entry : entries) {
                pstmt.setString(1, entry.apkHash);
                pstmt.setString(2, entry.apkName);
                pstmt.setString(3, entry.packageName);
                pstmt.setString(4, entry.version);
                pstmt.addBatch();
            }
            
            pstmt.executeBatch();
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    // 用于批量插入的数据结构
    public static class DependencyEntry {
        public final String apkHash;
        public final String apkName;
        public final String packageName;
        public final String version;
        
        public DependencyEntry(String apkHash, String apkName, String packageName, String version) {
            this.apkHash = apkHash;
            this.apkName = apkName;
            this.packageName = packageName;
            this.version = version;
        }
    }
}